package Ss1;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
public class Bmi1 extends Application{
    DataOutputStream toServer = null;
    DataInputStream fromServer = null;
    @Override
    public void start(Stage primaryStage) throws Exception {
        BorderPane paneForTextField = new BorderPane();
        paneForTextField.setPadding(new Insets(5, 5, 5, 5));
        paneForTextField.setStyle("-fx-border-color: green");
        paneForTextField.setLeft(new Label("身高: "));
        BorderPane paneForTextField1 = new BorderPane();
        paneForTextField1.setPadding(new Insets(5, 5, 5, 5));
        paneForTextField1.setStyle("-fx-border-color: green");
        paneForTextField1.setLeft(new Label("体重: "));

        Button btn = new Button("Left");
        btn.setText("计算'");
//更多请阅读：https://www.yiibai.com/javafx/javafx_button.html



        TextField tf = new TextField();
        tf.setAlignment(Pos.BOTTOM_RIGHT);
        paneForTextField.setCenter(tf);
        TextField tf1 = new TextField();
        tf1.setAlignment(Pos.BOTTOM_LEFT);
        paneForTextField1.setCenter(tf1);

        BorderPane mainPane = new BorderPane();
        TextArea ta = new TextArea();
        mainPane.setCenter(new ScrollPane(ta));
        mainPane.setTop(paneForTextField);
        mainPane.setLeft(paneForTextField1);
        mainPane.setBottom(btn);

        Scene scene = new Scene(mainPane, 450, 200);
        primaryStage.setTitle("BimClient");
        primaryStage.setScene(scene);
        primaryStage.show();

        btn.setOnAction(e -> {
            try {
                double height = Double.parseDouble(tf.getText().trim());
                double weight = Double.parseDouble(tf1.getText().trim());


                toServer.writeDouble(height);
                toServer.writeDouble(weight);
                toServer.flush();

                double bim = fromServer.readDouble();
                String str=fromServer.readUTF();

                ta.appendText("身高 " + height + "\n");
                ta.appendText("体重 " + weight + "\n");
                ta.appendText("bim "
                        + bim + '\n');
                ta.appendText(str);

            } catch (IOException ex) {
                System.err.println(ex);
            }
        });

        try {

            Socket socket = new Socket("localhost", 8000);

            fromServer = new DataInputStream(socket.getInputStream());

            toServer = new DataOutputStream(socket.getOutputStream());
        } catch (IOException ex) {
            ta.appendText(ex.toString() + '\n');
        }
    }

    public static void main(String[] args) {
        launch(args);
    }


}
